/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StaffEfficiencyPanel.java
 *
 * Created on Apr 13, 2010, 6:59:12 PM
 */

package edu.asu.cse360.client.ui.frame;

import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.model.ColumnModel;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.dto.StaffEfficiency;
import edu.asu.cse360.dto.StaffEfficiencyReport;
import edu.asu.cse360.model.Employee;
import edu.asu.cse360.service.StaffReportService;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import org.joda.time.DateTime;

/**
 *This class is to display the efficiency of each of the staff members for the manager to see
 * @author ggreiner
 */
public class StaffEfficiencyPanel extends javax.swing.JPanel {
    private static final String EMPLOYEE_TYPE_WAITPERSON = "Wait Person";
    private static final String EMPLOYEE_TYPE_CHEF = "Chef";
    private static final String DURATION_DAILY = "Daily";
    private static final String DURATION_WEEKLY = "Weekly";
    private static final String DURATION_MONTHLY = "Monthly";
    private static final String DURATION_ALL_TIME = "All Time";
    
    private DateTime startDate;
    private DateTime endDate;
    private ComboBoxModel durationModel;
    private StaffEfficiencyTableModel tableModel;

    private StaffEfficiencyColumnModel efficienyColumns;

    private String[] waitPersonColumns = new String[]{"Name", "Items Delivered"};
    private String[] chefPersonColumns = new String[]{"Name", "Items Cooked"};
    private StaffReportService staffReportSvc;

    /** Creates new form StaffEfficiencyPanel */
    public StaffEfficiencyPanel() {
        startDate = new DateTime();
        endDate = new DateTime();
        efficienyColumns = new StaffEfficiencyColumnModel(waitPersonColumns);
        durationModel = new DefaultComboBoxModel(new String[]{DURATION_DAILY, DURATION_WEEKLY, DURATION_MONTHLY, DURATION_ALL_TIME});
        tableModel = new StaffEfficiencyTableModel(efficienyColumns);
        staffReportSvc = ServiceFactory.getStaffReportService();
        initComponents();
        refreshTableModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        spinField = new com.toedter.components.JSpinField();
        durationBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        timeAgoLbl = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        employeeTypeBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        reportTable = new javax.swing.JTable();

        spinField.setMaximum(Integer.MAX_VALUE);
        spinField.setMinimum(0);
        spinField.setValue(1);
        spinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                spinFieldPropertyChange(evt);
            }
        });

        durationBox.setModel(durationModel);
        durationBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                durationBoxActionPerformed(evt);
            }
        });

        jLabel1.setText("Time Period");

        timeAgoLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeAgoLbl.setText("Ago");

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        employeeTypeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Wait Person", "Chef" }));
        employeeTypeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeTypeBoxActionPerformed(evt);
            }
        });

        jLabel2.setText("Employee Type");

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(29, 29, 29)
                        .add(employeeTypeBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(58, 58, 58)
                        .add(jLabel2)))
                .add(18, 18, 18)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(durationBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(37, 37, 37)
                        .add(jLabel1)))
                .add(18, 18, 18)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(timeAgoLbl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(spinField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 42, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel1)
                    .add(timeAgoLbl)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(durationBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(employeeTypeBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(spinField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        reportTable.setModel(getTableModel());
        jScrollPane1.setViewportView(reportTable);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 1080, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .add(116, 116, 116)
                .add(jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(433, 433, 433))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void spinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_spinFieldPropertyChange
        handleTimeSelectionChange();
}//GEN-LAST:event_spinFieldPropertyChange

    private void durationBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_durationBoxActionPerformed
        handleTimeSelectionChange();
}//GEN-LAST:event_durationBoxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    refreshTableModel();
}//GEN-LAST:event_jButton1ActionPerformed

    private void employeeTypeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_employeeTypeBoxActionPerformed

        if(EMPLOYEE_TYPE_WAITPERSON.equals(employeeTypeBox.getSelectedItem())){
            efficienyColumns.setColumns(waitPersonColumns);
        }else{
            efficienyColumns.setColumns(chefPersonColumns);
        }
        tableModel.fireTableStructureChanged();
    }//GEN-LAST:event_employeeTypeBoxActionPerformed


    private TableModel getTableModel(){
        return tableModel;
    }
    
    private void handleTimeSelectionChange() {
        int timeAgo = spinField.getValue();
        endDate = new DateTime();
        Object selectedItem = durationBox.getSelectedItem();
        spinField.getSpinner().setEnabled(true);
        if(selectedItem.equals(DURATION_DAILY)){
            startDate = endDate.minusDays(timeAgo);
            timeAgoLbl.setText("Days Ago");
        }else if(selectedItem.equals(DURATION_MONTHLY)){
            startDate = endDate.minusMonths(timeAgo);
            timeAgoLbl.setText("Months Ago");
        }else if(selectedItem.equals(DURATION_WEEKLY)){
            startDate = endDate.minusWeeks(timeAgo);
            timeAgoLbl.setText("Weeks Ago");
        }else if(selectedItem.equals(DURATION_ALL_TIME)){
            startDate = new DateTime(0L);
            spinField.getSpinner().setEnabled(false);
            timeAgoLbl.setText("All Time");
        }
    }

    private void refreshTableModel() {
        StaffEfficiencyReport report = staffReportSvc.getEfficiencyReport(getEmployeeType(), RestaurantContext.getRestaurant(), startDate, endDate);
        tableModel.setEfficiencies(report.getEfficiencies());
    }

    private String getEmployeeType(){
        return employeeTypeBox.getSelectedItem().toString().replace(" ", "").toLowerCase();
    }
    class StaffEfficiencyTableModel extends AbstractTableModel{
        private List<StaffEfficiency> efficiencies;
        private ColumnModel<StaffEfficiency> columns;

        public StaffEfficiencyTableModel(ColumnModel<StaffEfficiency> columns){
            this.columns = columns;
            this.efficiencies = new ArrayList<StaffEfficiency>();
        }

        public int getRowCount() {
            return efficiencies.size();
        }

        public int getColumnCount() {
            return columns.getCount();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            StaffEfficiency target = efficiencies.get(rowIndex);
            return columns.getValue(target, columnIndex);
        }

        @Override
        public String getColumnName(int columnIndex){
            return columns.getColumnName(columnIndex);
        }
        public void setEfficiencies(List<StaffEfficiency> efficiencies){
            this.efficiencies = efficiencies;
            super.fireTableDataChanged();
        }
    }

    class StaffEfficiencyColumnModel implements ColumnModel<StaffEfficiency>{
        private String[] currentColumns;

        public StaffEfficiencyColumnModel(String[] columns){
            this.currentColumns = columns;
        }
        public void setColumns(String[] columns){
            this.currentColumns = columns;
        }
        
        public Object getValue(StaffEfficiency target, int index) {
            Object value = null;
            switch(index){
                case 0:
                    Employee emp = target.getEmployee();
                    value = emp.getFirstName() + " "+emp.getLastName();
                    break;
                case 1:
                    value = target.getFrequency();
                    break;
            }
            return value;
        }

        public String getColumnName(int index) {
            return currentColumns[index];
        }

        public int getCount() {
            return currentColumns.length;
        }
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox durationBox;
    private javax.swing.JComboBox employeeTypeBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable reportTable;
    private com.toedter.components.JSpinField spinField;
    private javax.swing.JLabel timeAgoLbl;
    // End of variables declaration//GEN-END:variables

}
